package org.mao.forumtest;

import java.io.File;
import java.io.FileWriter;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ReadLwgod {

	private final static Logger logger;

	static {
		PropertyConfigurator.configure("D:/workspace/spiderman/src/main/resources/conf/log4j.properties");// 加载.xml文件
		logger = Logger.getLogger(ReadLwgod.class);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		logger.info("go");
		ReadLwgod read = new ReadLwgod();
		try {
			// read.readHomePage("http://www.lwgod.com/");
			read.readHomePageTxt("G:/download/lwgod/home.txt");
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	public void readHomePage(String url) throws Exception {
		File file = new File("G:/download/lwgod/home.txt");
		if (!file.getParentFile().exists()) {
			file.getParentFile().mkdirs();
		}
		FileWriter fw = new FileWriter(file);

		Document doc = Jsoup.connect(url).get();
		// logger.info(doc.html());
		fw.write(doc.html());
		fw.close();
	}

	public void readHomePageTxt(String path) throws Exception {
		Document doc = Jsoup.parse(new File(path), "UTF-8");
		Elements tables = doc.select("table.fl_tb");
		// logger.info(tables.size());
		if (CollectionUtils.isNotEmpty(tables)) {
			for (Element table : tables) {
				Elements trs = table.select("tbody>tr");
				// logger.info(trs.size());
				if (CollectionUtils.isNotEmpty(trs)) {
					for (Element tr : trs) {
						Elements tds = tr.select("td");
						if (CollectionUtils.isNotEmpty(tds)) {
							for (Element td : tds) {
								Elements links = td.select("a");
								if (CollectionUtils.isNotEmpty(links)) {
									Element link = links.get(0);
									Elements imgs = link.select("img");
									if (CollectionUtils.isNotEmpty(imgs)) {
										Element img = imgs.get(0);
										String insertSql = "insert into forum_section(forumId, name, code, url, createTime) values(2, '%s', '%s', '%s', '2017-09-10');";
										String name = img.attr("alt");
										String url = link.attr("abs:href");
										String code = link.attr("href");// forum-127-1.html
										if (StringUtils.isNotBlank(code)) {
											code = code.replace("-1.html", "");
										}
										insertSql = String.format(insertSql, name, code, url);
										System.out.println(insertSql);
									}
								}
							}
						}
					}
				}
			}
		}
	}

}
